package server.whiteboard;

import dao.BoardFileDAO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * 在线白板的状态管理
 * 暂时放这，后面看要不要放到redis
 */
public class WhiteBoardManager {

    private static HashMap<Long, WhiteBoardState> whiteBoardMap = new HashMap<>();

    public static void connectBoard(long boardId, String member, String address) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        if (whiteBoardState == null) {
            whiteBoardState = initWhiteBoardState(boardId);
        }
        whiteBoardState.onlineMembers.add(member);
        whiteBoardState.memberAddressSet.add(address);
    }

    public static void disconnectBoard(long boardId, String member, String address) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        if (whiteBoardState == null) {
            return;
        }
        whiteBoardState.onlineMembers.remove(member);
        whiteBoardState.memberAddressSet.remove(address);
        if (whiteBoardState.onlineMembers.isEmpty()) {
            whiteBoardMap.remove(boardId);
        }
    }


    public static List<String> getOnlineMembers(long boardId) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        if (whiteBoardState != null) {
            return new ArrayList<>(whiteBoardState.onlineMembers);
        }
        return new ArrayList<>();
    }

    public static List<String> getOnlineMemberAddress(long boardId) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        return  new ArrayList<>(whiteBoardState.memberAddressSet);
    }

    public static void addBoardShape(long boardId, List<String> updateShapeList) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        whiteBoardState.boardShapeList.addAll(updateShapeList);
    }

    public static List<String> getBoardCurrentShapes(long boardId) {
        return whiteBoardMap.get(boardId).boardShapeList;
    }

//    public static void saveBoard(long boardId) {
//        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
//        BoardFileDAO.ME.updateBoardContent(whiteBoardState.boardShapeList, boardId);
//    }

    public static void clearBoard(long boardId) {
        WhiteBoardState whiteBoardState = whiteBoardMap.get(boardId);
        whiteBoardState.boardShapeList.clear();
    }

    private static WhiteBoardState initWhiteBoardState(long boardId) {
        WhiteBoardState whiteBoardState = new WhiteBoardState(boardId);
        whiteBoardState.boardShapeList = BoardFileDAO.ME.getBoardShapeList(boardId);
        whiteBoardMap.put(boardId, whiteBoardState);
        return whiteBoardState;
    }

    private final static class WhiteBoardState {
        long board_id;
        HashSet<String> onlineMembers = new HashSet<>();
        HashSet<String> memberAddressSet = new HashSet<>();
        List<String> boardShapeList = new ArrayList<>();

        WhiteBoardState(long boardId) {
            this.board_id = boardId;
        }
    }
}
